package com.openlogic.j.completion;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;

/**
 * @ClassName: CompletionHeader
 * @Description: 请求头相应实体
 * @author weiyongpeng
 * @date 2025年2月21日 下午2:00:03
 */
@JsonDeserialize(builder = CompletionHeader.Builder.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class CompletionHeader {

	/**
	 * 是否匿名访问，默认false
	 */
	@JsonProperty
	private final Boolean isAnonymous;

	/**
	 * 请求token
	 */
	@JsonProperty
	private final String secret;

	/**
	 * 请求响应头类型 默认值 application/json
	 */
	@JsonProperty
	private final String contentType;

	public CompletionHeader(Builder builder) {
		this.isAnonymous = builder.isAnonymous;
		this.secret = builder.secret;
		this.contentType = builder.contentType;
	}

	public Boolean getIsAnonymous() {
		return isAnonymous == null ? Boolean.FALSE : isAnonymous;
	}

	public String getSecret() {
		return secret;
	}

	public String getContentType() {
		return StringUtils.isNotBlank(contentType) ? contentType : "application/json";
	}

	@Override
	public String toString() {
		return "CompletionHeader [isAnonymous=" + isAnonymous + ", secret=" + secret + ", contentType=" + contentType
				+ "]";
	}

	@Override
	public int hashCode() {
		return Objects.hash(contentType, isAnonymous, secret);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CompletionHeader other = (CompletionHeader) obj;
		return Objects.equals(contentType, other.contentType) && Objects.equals(isAnonymous, other.isAnonymous)
				&& Objects.equals(secret, other.secret);
	}

	public static Builder builder() {
		return new Builder();
	}

	@JsonPOJOBuilder(withPrefix = "")
	@JsonIgnoreProperties(ignoreUnknown = true)
	@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
	public static final class Builder {

		private Boolean isAnonymous = Boolean.FALSE;
		private String secret;
		private String contentType = "application/json";

		private Builder() {
		}

		public Builder from(CompletionHeader header) {
			isAnonymous(header.isAnonymous);
			secret(header.secret);
			contentType(header.contentType);
			return this;
		}

		public Builder isAnonymous(Boolean isAnonymous) {
			this.isAnonymous = isAnonymous;
			return this;
		}

		public Builder secret(String secret) {
			this.secret = secret;
			return this;
		}

		public Builder contentType(String contentType) {
			this.contentType = contentType;
			return this;
		}

		public CompletionHeader build() {
			return new CompletionHeader(this);
		}
	}

}
